/*
 * Copyright (c) 18/5/19 foo.com. All Rights Reserved.
 */
package com.cgmnx.admin.web.converter.vm;

import com.cgmnx.admin.dao.entity.NoteEntity;
import com.cgmnx.admin.model.enums.NodeAuditStatus;
import com.cgmnx.admin.model.enums.NodeType;
import com.cgmnx.admin.web.converter.Convert;
import com.cgmnx.admin.web.model.vm.NoteVm;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: z Date: 18/5/19 Time: 下午7:51
 * @version: 1.0.0
 */
@Component
public class NoteVmConverter implements Convert<NoteEntity, NoteVm> {

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");


    @Override
    public NoteVm convert(NoteEntity t) {

        if (t == null) {
            return null;
        }

        NoteVm vm = new NoteVm();

        vm.setCityId(t.getCityId());
        vm.setBrandType(t.getBrand ());
        vm.setMoney(t.getCurrency ());
        vm.setRate(t.getPrice ());
        vm.setCountry(t.getCountry());
        vm.setId(t.getId());
        vm.setUpdateUser(t.getUpdateUser());
        vm.setCreateUser(t.getCreateUser());
        vm.setContent(t.getContent());

        final NodeAuditStatus status = t.getStatus();
        if (status != null) {
            vm.setStatusCode(status.getCode());
            vm.setStatusText(status.getDesc());
        }

        final NodeType type = t.getType();
        if (type != null) {
            vm.setTypeCode(type.getCode());
            vm.setTypeText(type.getDesc());
        }


        vm.setImages(t.getImages());
        vm.setTitle(t.getTitle());
        vm.setAuditTime(DATE_TIME_FORMATTER.format(LocalDateTime.ofInstant(t.getAuditTime().toInstant(), ZoneId.systemDefault())));
        vm.setCreateTime(DATE_TIME_FORMATTER.format(LocalDateTime.ofInstant(t.getCreateTime().toInstant(), ZoneId.systemDefault())));
        vm.setUpdateTime(DATE_TIME_FORMATTER.format(LocalDateTime.ofInstant(t.getUpdateTime().toInstant(), ZoneId.systemDefault())));

        return vm;
    }

    @Override
    public List<NoteVm> convert(List<NoteEntity> t) {
        return t.stream().map(this::convert).collect(Collectors.toList());

    }
}
